//
//  LWDetailsViewController.m
//  LWShopItem
//
//  Created by mac on 17/4/3.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWDetailsViewController.h"
#import "UIColor+Addition.h"
#import "LWDetailModel.h"
#import <AFNetworking.h>
#import <CommonCrypto/CommonCrypto.h>
#import <UIImageView+WebCache.h>
#import <Masonry.h>
#import "LWDetailModel.h"
#import "LWDetilsCell.h"
#import <WebKit/WebKit.h>


static NSString *cellIID = @"cellIID";
static NSString *detilCell = @"detilCell";

@interface LWDetailsViewController ()

@property (strong,nonatomic)  UIImageView   *topImage;      //顶部图片
@property (strong,nonatomic)  WKWebView     *webView;       //底部图片
@property (strong,nonatomic)  UILabel       *nameLabel;     //名称
@property (strong,nonatomic)  UILabel       *tagLabelL;     //羊角标签
@property (strong,nonatomic)  UILabel       *nowPriceLabel; //现在价格
@property (strong,nonatomic)  UILabel       *originalPriceLabel; //原价
@property (strong,nonatomic)  UILabel       *imagetextLab;  //折扣
@property (strong,nonatomic)  UILabel       *discountLabel; //详情折扣

@end

@implementation LWDetailsViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadData];
    
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIID];
    [self.tableView registerClass:[LWDetilsCell class] forCellReuseIdentifier:detilCell];
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
        
    }else if (section == 1)
    {
        return 1;
    }else
    {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        LWDetilsCell *cell = [tableView dequeueReusableCellWithIdentifier:detilCell forIndexPath:indexPath];
        
        
        cell.backgroundColor = [UIColor colorWithHex:0xF2F2F2];
        
        return cell;
    }else
    {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIID forIndexPath:indexPath];
        
        
        
        
        return cell;
        
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat rowHieght;
    if (indexPath.section == 0) {
        rowHieght = 90;
    }
    else if(indexPath.section == 1)
    {
        rowHieght = 140;
        
    }else if (indexPath.section == 2)
    {
        return 800;
    }
    
    return  rowHieght;
}



#pragma mark - 加载数据
- (void)loadData
{
    
    NSString *secrt = @"shop.sdlinwang.com";
    
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval interval = [date timeIntervalSince1970];
    NSString *dateString = [NSString stringWithFormat:@"%0.f",interval];
    
    NSString *sing = [NSString stringWithFormat:@"%@%@%@",self.itemId,dateString,secrt];
    
    AFHTTPSessionManager  *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dicArr = @{@"id":self.itemId,@"time":dateString,@"sign":[self hybnetworking_md5:sing]};
    
    [manager POST:@"http://shop.sdlinwang.com/index.php?m=Api&c=Goods&a=goodsInfo" parameters:dicArr progress:nil success:^(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject){
        
        NSLog(@"请求成功:%@", responseObject);
        
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        NSLog(@"请求成功JSON:%@", JSON);
        
        NSDictionary *tempDict = JSON[@"result"][@"goods"];
        
        //字典转模型
        LWDetailModel *detailModel = [LWDetailModel detilWtihDict:tempDict];
        
        //赋值
        [self.topImage sd_setImageWithURL:[NSURL URLWithString:detailModel.original_img]];
        
        
        
        NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://shop.sdlinwang.com/index.php/Mobile/Goods/goodsInfo2/id/%@",detailModel.goods_id]]];
        
        [self.webView loadRequest:request];
        
        
        NSLog(@"%@",[NSURL URLWithString:[NSString stringWithFormat:@"http://shop.sdlinwang.com/index.php/Mobile/Goods/goodsInfo2/id/%@",detailModel.goods_id]]);
        
        
        
        self.nameLabel.text = detailModel.goods_name;
        
        self.nowPriceLabel.text = detailModel.shop_price;
        
        self.originalPriceLabel.text = detailModel.market_price;
        
        self.discountLabel.text = detailModel.discount;
        
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        
        NSLog(@"%@",error);
        
        
        
    }];
    
}

#pragma MARK - MD5加密
- (NSString *)hybnetworking_md5:(NSString *)string {
    if (string == nil || [string length] == 0) {
        return nil;
    }
    
    unsigned char digest[CC_MD5_DIGEST_LENGTH], i;
    CC_MD5([string UTF8String], (int)[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding], digest);
    NSMutableString *ms = [NSMutableString string];
    
    for (i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [ms appendFormat:@"%02x", (int)(digest[i])];
    }
    
    return [ms copy];
}


#pragma MARK - UI
- (void)setupUI
{
    self.view.backgroundColor = [UIColor colorWithHex:0xF2F2F2];
    
    self.title = @"商品详情";
    
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(popClick)];
    self.navigationItem.leftBarButtonItem = backBtnItem;
    
    //顶部图片
    UIImageView *topImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 280)];
    self.topImage = topImage;
    self.tableView.tableHeaderView = topImage;
    
    //商品名称
    UILabel *nameLabel = [UILabel new];
    nameLabel.font = [UIFont systemFontOfSize:18];
    nameLabel.numberOfLines = 0;
    self.nameLabel = nameLabel;
    [self.view addSubview:nameLabel];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(topImage.mas_bottom).offset(0);
        
        make.height.mas_equalTo(60);
        
        make.width.equalTo(self.view).offset(-20);
        
        make.left.equalTo(self.view).offset(15);
        
    }];
    
    //羊角标签
    UILabel *tagLabelL = [UILabel new];
    tagLabelL.text = @"￥:";
    tagLabelL.font = [UIFont systemFontOfSize:24];
    tagLabelL.textColor = [UIColor redColor];
    self.tagLabelL = tagLabelL;
    [self.view addSubview:tagLabelL];
    
    [tagLabelL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nameLabel.mas_bottom).offset(60);
        
        make.left.equalTo(self.view).offset(15);
        
    }];
    
    //现在价格
    UILabel *nowPriceLabel = [UILabel new];
    nowPriceLabel.font = [UIFont systemFontOfSize:24];
    nowPriceLabel.textColor = [UIColor redColor];
    self.nowPriceLabel = nowPriceLabel;
    [self.view addSubview:nowPriceLabel];
    
    [nowPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nameLabel.mas_bottom).offset(60);
        
        make.left.equalTo(tagLabelL.mas_right).offset(5);
        
    }];
    
    //羊角标签
    UILabel *tagLabelR = [UILabel new];
    tagLabelR.text = @"原价:￥";
    tagLabelR.font = [UIFont systemFontOfSize:15];
    self.tagLabelL = tagLabelL;
    [self.view addSubview:tagLabelR];
    
    [tagLabelR mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(nowPriceLabel.mas_bottom).offset(-5);
        
        make.left.equalTo(nowPriceLabel.mas_right).offset(20);
        
    }];
    
    //原价
    UILabel *originalPriceLabel = [UILabel new];
    originalPriceLabel.font = [UIFont systemFontOfSize:15];
    self.originalPriceLabel = originalPriceLabel;
    [self.view addSubview:originalPriceLabel];
    
    [originalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(nowPriceLabel.mas_bottom).offset(-5);
        
        make.left.equalTo(tagLabelR.mas_right).offset(0);
        
    }];
    
    //掩饰线条
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor redColor];
    [self.view addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(originalPriceLabel);
        
        make.width.equalTo(originalPriceLabel).offset(20);
        
        make.height.mas_equalTo(1);
    }];
    
    //折扣
    UILabel *imagetextLab = [UILabel new];
    imagetextLab.text = @"折扣:";
    imagetextLab.font = [UIFont systemFontOfSize:16];
    self.imagetextLab = imagetextLab;
    [self.view addSubview:imagetextLab];
    
    [imagetextLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nowPriceLabel.mas_bottom).offset(20);
        
        make.left.equalTo(self.view).offset(15);
        
    }];
    
    //详情折扣
    UILabel *discountLabel = [UILabel new];
    discountLabel.font = [UIFont systemFontOfSize:16];
    self.discountLabel = discountLabel;
    [self.view addSubview:discountLabel];
    
    [discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nowPriceLabel.mas_bottom).offset(20);
        
        make.left.equalTo(imagetextLab.mas_right).offset(10);
        
    }];
    
    //图文详情
    UILabel *photographLabel = [UILabel new];
    photographLabel.font = [UIFont systemFontOfSize:15];
    photographLabel.text = @"图文详情:";
    [self.view addSubview:photographLabel];
    
    [photographLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(imagetextLab.mas_bottom).offset(15);
        
        make.left.equalTo(self.view).offset(15);
    }];
    
    //底部图片
    WKWebView *webView = [[WKWebView alloc] init];
    // [webView setFrame:CGRectMake(0, 0, self.view.frame.size.width,280)];
    self.webView = webView;
    [self.view addSubview:webView];
    
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(photographLabel.mas_bottom).offset(10);
        
        make.left.right.equalTo(self.view);
        
        make.width.equalTo(self.view);
        
        make.height.mas_equalTo(800);
    }];
    
}

#pragma MARK - POP
- (void)popClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
